﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_UserHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int roleId = 0;
            int userId = 0;
            if (Session != null && Session["UserId"] != null)
            {
                roleId = Convert.ToInt32(Session["RoleId"]);
                userId = Convert.ToInt32(Session["UserId"]);
                CallListUser(roleId, userId);               
            }
        }
    }

    private void CallListUser(int roleId, int userId)
    {
        DataTable dt = new DataTable();
        dt = DataAccessClass.ListUser(roleId, userId);
        if (dt.Rows.Count == 1)
        {
            txtAddress.Text = Convert.ToString(dt.Rows[0]["Address"]);
            txtFullName.Text = Convert.ToString(dt.Rows[0]["Name"]);
            lblUserName.Text = Convert.ToString(dt.Rows[0]["UserName"]);
        }
    }
    protected void Update(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Request.QueryString["UserId"]);
            int result = 0;
            result = DataAccessClass.UpdateUserProfile(userId, txtFullName.Text, txtAddress.Text);
            if (result > 0)
            {
                trMessage.Visible = true;
                trError.Visible = false;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;
            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }
    
   
    protected void btnUpdatePwd_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Request.QueryString["UserId"]);
            if (userId==0)
            {
               userId = Convert.ToInt32(Session["UserId"]); 
            }
            int result = 0;
            result = DataAccessClass.UpdatePassword(userId, txtNewPwd.Text);
            if (result > 0)
            {
                trMessage.Visible = true;
                trError.Visible = false;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;
            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }
}